home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Publishing / ImagePortfolio / Source / FileUtils.m < prev    next >
Text File  |  1994-04-01  |  4KB  |  114 lines

  1. // -------------------------------------------------------------------------------------
  2. // FileUtils.m - file manipulation utilities
  3. // Martin D. Flynn, NeXT Computer, Inc.
  4. // You may freely copy, distribute and reuse the code in this example.
  5. // NeXT disclaims any warranty of any kind, expressed or implied, as to its
  6. // fitness for any particular use.
  7. // -------------------------------------------------------------------------------------
  8.  
  9. #import <stdlib.h>
  10. #import <string.h>
  11. #import <libc.h>
  12. #import <sys/dir.h>             // for getdirentries()
  13. #import <objc/objc-class.h>        // for class_getInstanceMethod()
  14. #import <objc/hashtable.h>        // for NXCopyStringBuffer()
  15. #import "FileUtils.h"
  16.  
  17. // -------------------------------------------------------------------------------------
  18. // return the number of arguments taken by the specified method
  19. @implementation Object(methodNumberOfArgs)
  20. static int _methodNumberOfArgs(Object *obj, SEL aSelector)
  21. {
  22.     Method    m = class_getInstanceMethod(obj->isa, aSelector);
  23.     if (!m) m = class_getClassMethod(obj->isa, aSelector);
  24.     return (m)? method_getNumberOfArguments(m) - 2: -1;
  25. }
  26. @end
  27.  
  28. // -------------------------------------------------------------------------------------
  29. @implementation FileUtils : Object
  30.  
  31. // -------------------------------------------------------------------------------------
  32. // file directory from fully qualified path/file
  33. // -------------------------------------------------------------------------------------
  34.  
  35. /* send all files found in a specified directory to the specified action method */
  36. + sendFileDirectory:(const char*)dirPath toTarget:target method:(SEL)method with:anArg
  37. {
  38.     long          basep;
  39.     char          *buf;
  40.     struct direct *dp;
  41.     int           argc, cc, fd;
  42.     char          dirbuf[8192];
  43.     char          fullPath[MAXPATHLEN];
  44.   
  45.     /* open file path */
  46.     fd = open(dirPath, O_RDONLY, 0644);
  47.     if (fd <= 0)return (id)nil;
  48.  
  49.     /* get number of accepted args */
  50.     argc = _methodNumberOfArgs(target, method);
  51.   
  52.     /* open file path */
  53.     cc = getdirentries(fd, (buf = dirbuf), sizeof(dirbuf), &basep);
  54.     while (cc) {
  55.         dp = (struct direct *)buf;
  56.         if (*(dp->d_name) && strcmp(dp->d_name, ".") && strcmp(dp->d_name, "..")) {
  57.             sprintf(fullPath, "%s/%s", dirPath, dp->d_name);
  58.             if (argc == 1) [target perform:method with:(void*)fullPath];
  59.             else [target perform:method with:(void*)fullPath with:anArg];
  60.         }
  61.         buf += dp->d_reclen;
  62.         if (buf >= dirbuf + cc)
  63.             cc = getdirentries(fd, (buf = dirbuf), sizeof(dirbuf), &basep);
  64.     }
  65.  
  66.     /* close path and return list */
  67.     close(fd);
  68.     return self;
  69.   
  70. }
  71.  
  72. // -------------------------------------------------------------------------------------
  73. // extract file name from fully qualified path/file
  74. // -------------------------------------------------------------------------------------
  75.  
  76. /* extract 'name.ext' from fully qualified file name */
  77. + (const char*)extractFileNameExt:(const char*)fileName
  78. {
  79.     char    *temp;
  80.     return (temp=rindex(fileName, '/'))? temp + 1 : fileName;
  81. }
  82.  
  83. /* extract '.ext' from fully qualified file name */
  84. + (const char*)extractFileExt:(const char*)fileName
  85. {
  86.     char        *temp;
  87.     static char    *nullStr = "";
  88.     return (temp=rindex(fileName, '.'))? temp : nullStr;
  89. }
  90.  
  91. /* extract and copy file from fully qualified file name */
  92. + (char*)copyFileName:(const char*)fileName
  93. {
  94.     char    name[MAXPATHLEN], *temp;
  95.     strcpy(name, fileNAMEXT(fileName));
  96.     if (temp=rindex(name, '.')) *temp = 0;
  97.     return NXCopyStringBuffer(name);
  98. }
  99.  
  100. /* extract and copy file from fully qualified file name */
  101. + (char*)copyFilePath:(const char*)fileName
  102. {
  103.     int        len;
  104.     char    *temp, *path;
  105.     if (!(temp=rindex(fileName, '/'))) return (char*)nil;
  106.     len = temp - fileName;
  107.     path = (char*)malloc(len + 1);
  108.     strncpy(path, fileName, len);
  109.     path[len] = 0;
  110.     return path;
  111. }
  112.  
  113. @end
  114.